import { createSlice, Draft, PayloadAction } from "@reduxjs/toolkit";
import { persistReducer } from "redux-persist";
import storage from "redux-persist/lib/storage";

export interface ConsultState {
  consult: PartialConsult;
}

export const consultSlice = createSlice<
  ConsultState,
  {
    // 记录问诊信息
    saveConsult(
      state: Draft<ConsultState>,
      action: PayloadAction<PartialConsult>
    ): void;
    // 清空问诊记录
    clearConsult(state: Draft<ConsultState>): void;
  },
  "consult"
>({
  name: "consult",
  initialState: {
    // 问诊信息
    consult: {},
  },
  reducers: {
    // 记录问诊信息
    saveConsult(state, action) {
      state.consult = {
        ...state.consult,
        ...action.payload,
      };
    },
    // 清空问诊记录
    clearConsult(state) {
      state.consult = {};
    },
  },
})
// 持久化
export const consultReducer = persistReducer(
    { key: consultSlice.name, storage },
    consultSlice.reducer
  );
  
  export const { saveConsult, clearConsult } = consultSlice.actions;